// 监听数据
import { contractFundsTable } from "@/content-scripts/components/fund/contractFunds/contractFunds";

// 合约账户
export function observeFundsDetails() {
  let previousValues = {
    perpetualBtn: null,
    deliveryBtn: null,
  };

  const observer = new MutationObserver(() => {
    // console.log(mutations,'mutations');
    const btnElement = document.querySelectorAll(".mantine-ka9rmf");
    const perpetualBtn = btnElement[0];
    const deliveryBtn = btnElement[1];

    // 检查是否有值发生变化
    const hasChanged =
      perpetualBtn !== previousValues.perpetualBtn ||
      deliveryBtn !== previousValues.deliveryBtn;

    // 如果所有必需的元素都存在且值发生变化
    if (perpetualBtn && deliveryBtn && hasChanged) {
      // 存新值
      previousValues = {
        perpetualBtn,
        deliveryBtn,
      };
      console.log("值发生永续，交割变化：", perpetualBtn, deliveryBtn);
      contractFundsTable(previousValues);
    }
  });

  // 监听整个文档的变化
  observer.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: true,
    characterData: true,
  });
}

// 创新区账户
import { innovationFundsTable } from "@/content-scripts/components/fund/innovationFunds/innovationFunds";
export function observeInnovationDetails() {
  // 获取初始值
  let previousValues = {
    searchInput: null,
    changeBtn: null,
  };

  const observer = new MutationObserver(() => {
    // console.log(mutations,'mutations');
    const searchInput = document.querySelector(
      ".mantine-1xeva5p > .mantine-yqpz0l"
    );
    const changeBtn = document.querySelector(
      '.mantine-ujq11l .mantine-1c60rwd[data-active="true"]'
    );

    // 检查是否有值发生变化
    const hasChanged =
      searchInput !== previousValues.searchInput ||
      changeBtn !== previousValues.changeBtn;

    // 如果所有必需的元素都存在且值发生变化
    if (searchInput && changeBtn && hasChanged) {
      // 存新值
      previousValues = {
        searchInput,
        changeBtn,
      };
      console.log("创新区变化：", searchInput, changeBtn);
      innovationFundsTable({
        ...previousValues,
        searchInput: previousValues.searchInput.value,
      });
    }
  });

  // 监听整个文档的变化
  observer.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: true,
    characterData: true,
  });
}

import { leverageFunds } from "@/content-scripts/components/fund/leverageFunds/leverageFunds";
// 逐仓杠杆账户
let currentObserver = null;
export function observeLeverageDetails() {
  // 获取初始值
  let previousValues = {
    assetLiabilityLabel: null,
    textContent: null,
  };

  if (currentObserver) {
    currentObserver.disconnect();
  }
  currentObserver = new MutationObserver(() => {
    const assetLiabilityLabel =
      document.querySelector(
        '.mantine-ujq11l > .mantine-ka9rmf[data-active="true"]'
      ) ||
      document.querySelector(
        '.mantine-1tqz3t9 > .mantine-1fisquk[data-active="true"]'
      );
    // const changeBtn = document.querySelector(
    //   '.mantine-ujq11l .mantine-1c60rwd[data-active="true"]'
    // );
    if (assetLiabilityLabel) {
      let currentText = assetLiabilityLabel.textContent.trim();
      const textChange = currentText !== previousValues.textContent;
      if (textChange) {
        previousValues = {
          assetLiabilityLabel,
          textContent: currentText,
        };
        console.log(
          assetLiabilityLabel.textContent.trim(),
          "assetLiabilityLabel.textContent.trim()"
        );

        if (assetLiabilityLabel.textContent.trim() === "账户资产") {
          // 账户资产
          leverageFunds(previousValues);
          observeLeverageTable(); //监听表格搜索条件
        } else {
          // 负债管理
        }
      }
    }
  });
  currentObserver.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: true,
    characterData: true,
  });
}

import { leverageFundTable } from '@/content-scripts/components/fund/leverageFunds/leverageFunds.js'

let leverageTableObserver = null;
function observeLeverageTable() {
  if (leverageTableObserver) leverageTableObserver.disconnect();
  let previousValues = {
    searchInput: null,
    radioInput: null,
  };

  const pageTable = document.querySelector(".mantine-xlwgkm");
  console.log(pageTable, "PAGEtABLE");
  leverageTableObserver = new MutationObserver(() => {
    const searchInput =
      document.querySelector(".mantine-23sqbn > mantine-yqpz0l") ||
      document.querySelector(".mantine-23sqbn > .mantine-yqpz0l") ||
      document.querySelector(".mantine-23sqbn > input");
    console.log(searchInput.value, "sssseeeeaaaarrrcccchhhh");
    const radioInput =
      document.querySelector(".mantine-1rs5v5x > .mantine-msyjly") ||
      document.querySelector(".mantine-1rs5v5x > #mantine-R4omlamm6") ||
      document.querySelector(".mantine-1rs5v5x > input");
    console.log(radioInput.checked, "raddio111checked");
    const hasChanged =
      searchInput.value !== previousValues.searchInput ||
      radioInput.checked !== previousValues.radioInput;
    console.log(previousValues, 'previvvvvvvvvvv');
    console.log(searchInput.value, radioInput['checked'], '987536951');

    console.log(hasChanged, 'has');
    console.log(searchInput && radioInput && hasChanged, 'searchInput && radioInput && hasChanged');


    if (searchInput && radioInput && hasChanged) {


      previousValues = {
        searchInput: searchInput.value,
        radioInput: radioInput.checked
      }
      leverageFundTable({ ...previousValues, type: 'accountAssets' })
    }
  });

  leverageTableObserver.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: true,
    characterData: true,
  });
}

// let leverageLiabilityObserver = null

export function observeLeverageLiability() {
  if (leverageTableObserver) leverageTableObserver.disconnect();
  let previousValues = {
    tabBtn: null,
    debtInputSearch: null
  };

  leverageTableObserver = new MutationObserver(() => {
    const tabBtn = document.querySelector('.mantine-997qrr > .mantine-1dg49la')
    console.log(tabBtn, 'changeBtnchangeBtnchangeBtnchangeBtnchangeBtn');
    const debtInputSearch = document.querySelector('.mantine-dcm9q0 > .mantine-qqppja')
    console.log(debtInputSearch, 'debtInputSearchdebtInputSearchdebtInputSearchdebtInputSearch');


    const hasChanged =
      tabBtn.innerHTML === "当前负债" ? tabBtn.innerHTML !== previousValues.tabBtn : tabBtn.innerHTML === "负债明细" ? tabBtn.innerHTML !== previousValues.tabBtn || debtInputSearch.value !== previousValues.debtInputSearch : false;

    console.log(previousValues, 'previvvvvvvvvvv');

    console.log(hasChanged, 'has');


    if (tabBtn && (tabBtn.innerHTML === "当前负债" ? hasChanged : tabBtn.innerHTML === "负债明细" ? debtInputSearch && hasChanged : false)) {
      previousValues = {
        tabBtn: tabBtn.innerHTML,
        debtInputSearch: debtInputSearch.value || debtInputSearch
      }
      leverageFundTable({ ...previousValues, type: tabBtn.innerHTML == '当前负债' ? 'currentLiabilities' : 'debtDetails' })
    }
  });

  leverageTableObserver.observe(document.body, {
    childList: true,
    subtree: true,
    attributes: true,
    characterData: true,
  });

}
